package cn.toleo.member.service.impl;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.Session;
import javax.jms.TextMessage;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.core.MessageCreator;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import cn.toleo.common.pojo.EasyUIDataGridResult;
import cn.toleo.common.pojo.ToleoResult;
import cn.toleo.jedis.JedisClient;
import cn.toleo.mapper.TbItemDescMapper;
import cn.toleo.mapper.TbItemMapper;
import cn.toleo.pojo.TbItem;
import cn.toleo.pojo.TbItemDesc;
import cn.toleo.pojo.TbItemExample;
import cn.toleo.service.ItemService;
import cn.toleo.utils.IDUtils;
import cn.toleo.utils.JsonUtils;


/**
 * 商品管理Service
 */
@Service
public class ItemServiceImpl implements ItemService {
	@Autowired
	private JedisClient jedisClient;
	@Value("${ITEM_INFO}")
	private String ITEM_INFO;
	@Value("${ITEM_EXPIRE}")
	private int ITEM_EXPIRE;

	@Autowired
	private TbItemMapper itemMapper;
	
	@Autowired
	private TbItemDescMapper itemDescMapper;
	
	@Autowired
	private JmsTemplate jmsTemplate;
	@Resource(name="itemAddtopic")
	private Destination destination;
	
	
	@Override
	public TbItem getItemById(long itemId) {
		//先查缓存
		try{
			String json=jedisClient.get(ITEM_INFO+":"+itemId+":BASE");
			if(StringUtils.isNotBlank(json)){
				//有缓存转pojo
				TbItem tbItem = JsonUtils.jsonToPojo(json, TbItem.class);
				return tbItem;
			}
		}catch(Exception e){
			e.printStackTrace();
		}
		
		TbItem item = itemMapper.selectByPrimaryKey(itemId);
		//无缓存把从数据库取到的值放入缓存中
		try{
			//主干
			jedisClient.set(ITEM_INFO+":"+itemId+":BASE",JsonUtils.objectToJson(item));
			//时间过期
			jedisClient.expire(ITEM_INFO+":"+itemId+":BASE", ITEM_EXPIRE);
		}catch(Exception e){
			e.printStackTrace();
		}
		return item;
	}

	@Override
	public EasyUIDataGridResult getItemList(int page, int rows) {
		//获取分页信息
		PageHelper.startPage(page, rows);
		//执行查询
		TbItemExample example = new TbItemExample(); 
		List<TbItem> list = itemMapper.selectByExample(example);
		//取查询结果
		PageInfo<TbItem> pageInfo = new PageInfo<>(list);
		EasyUIDataGridResult result = new EasyUIDataGridResult();
		result.setRows(list);
		result.setTotal(pageInfo.getTotal());
		//返回结果
		return result;
	}
	
	@Override
	public ToleoResult addItem(TbItem item, String desc){
		//生产商品id
		final long itemId = IDUtils.genItemId();
		
		//补全item属性
		item.setId(itemId);
		
		//商品状态 1正常 2下架 3删除
		item.setStatus((byte)1);
		item.setCreated(new Date());
		item.setUpdated(new Date());
		//向商品表插入数据
		itemMapper.insert(item);
		//创建商品描述的pojo
		TbItemDesc itemDesc = new TbItemDesc();
		//补全pojo属性
		itemDesc.setItemId(itemId);
		itemDesc.setItemDesc(desc);
		itemDesc.setUpdated(new Date());
		itemDesc.setCreated(new Date());
		//向商品描述表插入数据
		itemDescMapper.insert(itemDesc);
		//向activemq发送商品添加消息
		jmsTemplate.send(destination,new MessageCreator() {
			
			@Override
			public Message createMessage(Session session) throws JMSException {
				// 发送商品id
				TextMessage textMessage = session.createTextMessage(itemId+"");
				
				return textMessage;
			}
		});
		
		//返回结果
		return ToleoResult.ok();
	}

	@Override
	public TbItemDesc geTbItemDescById(long itemId) {
		//先查缓存,有缓存转pojo结束,无缓存下一步
				try{
					String json=jedisClient.get(ITEM_INFO+":"+itemId+":DESC");
					if(StringUtils.isNotBlank(json)){
						//有缓存转pojo
						TbItemDesc tbItemDesc = JsonUtils.jsonToPojo(json, TbItemDesc.class);
						return tbItemDesc;
					}
				}catch(Exception e){
					e.printStackTrace();
				}
				
				
		//无缓存查数据库并把从数据库取到的值放入缓存中		
		TbItemDesc itemDesc = itemDescMapper.selectByPrimaryKey(itemId);
				try{
					//主干
					jedisClient.set(ITEM_INFO+":"+itemId+":DESC",JsonUtils.objectToJson(itemDesc));
					//时间过期
					jedisClient.expire(ITEM_INFO+":"+itemId+":DESC", ITEM_EXPIRE);
				}catch(Exception e){
					e.printStackTrace();
				}
		return itemDesc;
	}
	

}
